package com.vvv.designpattern.bridge.abst.factory;

import com.vvv.designpattern.bridge.abst.AbstractRegisterLoginComponent;
import com.vvv.designpattern.bridge.abst.RegisterLoginComponent;
import com.vvv.designpattern.bridge.function.RegisterLoginFuncInterface;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author vvv
 * @date 2023-11-16 15 41
 * discription
 */
public class RegisterLoginComponentFactory {
    /**
     * 缓存左路的实现类
     */
    public static Map<String, AbstractRegisterLoginComponent> componentMap =
            new ConcurrentHashMap<>();
    /**
     * 缓存右路的不同的实现类
     */
    public static Map<String, RegisterLoginFuncInterface> funcMap =
            new ConcurrentHashMap<>();

    /**
     * 根据不同的登录类型去获取对应的AbstractRegisterLoginComponent
     * @param type
     * @return
     */
    public static AbstractRegisterLoginComponent getComponent(String type) {
        /**
         * 这里需要保证AbstractRegisterLoginComponent是单例的所以要用double-check的方式
         *
         */
        AbstractRegisterLoginComponent component = componentMap.get(type);
        if (component != null) {
            return component;
        }
        synchronized (componentMap) {
            component = componentMap.get(type);
            if (component != null) {
                return component;
            }
            component = new RegisterLoginComponent(funcMap.get(type));
            componentMap.put(type, component);
        }
        return component;
    }
}
